/******************************************************************************
 * SAGE - Scalable Adaptive Graphics Environment
 *
 * Module: sageAudioModule.h
 * Author : Hyejung Hur
 * 
 * Copyright (C) 2004 Electronic Visualization Laboratory,
 * University of Illinois at Chicago
 *
 * This library is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either Version 2.1 of the License, or
 * (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
 * License for more details.
 *
 * You should have received a copy of the GNU Lesser Public License along
 * with this library; if not, write to the Free Software Foundation, Inc.,
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 *
 * Direct questions, comments etc about SAGE to bijeong@evl.uic.edu or 
 * http://www.evl.uic.edu/cavern/forum/
 *
 ***************************************************************************************************************************/

#ifndef _SAGE_AUDIO_MODULE_H
#define _SAGE_AUDIO_MODULE_H

#include <vector>

#include "sageBase.h"
#include "audioCircBuf.h"
#include "audioFileReader.h"
#include "audioFileWriter.h"
#include "audioFormatManager.h"

class sailConfig; 
class sageAudio;

class sageAudioConfig {
public:
	int  deviceNum;
	sageSampleFmt sampleFmt;
	long samplingRate;
	int  channels;
	int  framePerBuffer;
	sageAudioMode audioMode;
	
	sageAudioConfig() : deviceNum(-1), sampleFmt(SAGE_SAMPLE_FLOAT32), samplingRate(44100), channels(2), framePerBuffer(1024), audioMode(SAGE_AUDIO_CAPTURE) {}
	~sageAudioConfig() { }
};

class sageAudioModule {
public:
	/** this class is singleton
	*/
	static sageAudioModule* _instance;
	
	/** create instance and assign it to _instance
	*/
	static sageAudioModule* instance();
	
	/** set paramters for audio configuration 
	*/
	void init(sailConfig &conf);
	virtual void init(sageAudioConfig &conf);
	void updateConfig(sageAudioConfig &conf);
	
	audioCircBuf* load(char* filename, bool loop, long totalframes=0);
	int save(char* filename);

	/** open audio stream and play
	*/
	int play(int id);
	
	int pause(int id);
	int stop(int id);

	audioCircBuf* createObject(sageAudioConfig &conf);	
	// temporaly....
	audioCircBuf* createObject();	
	int deleteObject(int id);
	
	audioCircBuf* createBuffer();		
	audioCircBuf* createBuffer(sageAudioConfig &conf);	
	
	/** test audio input/output device
	*/
	void testDevices();

	~sageAudioModule();
	
	void setgFrameNum(long frames);
	long getgFrameNum();
			
protected:
	sageAudioModule();
	
	/** create audio and create circular buffer for saving audio block 
	*/	
	void init();	

protected:
	/** audio pointer
	*/
	//sageAudio* audio;
	std::vector<sageAudio*> audioList;
	
	/** audio circular buffer list
	*/
	std::vector<audioCircBuf*> bufferList;
	
	/** audio configuration
	*/
	sageAudioConfig config;
	
	audioFileReader* fileReader;
	audioFileWriter* fileWriter;
	audioFormatManager* formatManager;
	
	long gFrameNum;

}; 

#endif
